package com.zhihu_lvliangi_android.option.mycircle.parents;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhihu_lvliangi_android.R;
import com.zhihu_lvliangi_android.domain.HealthRecord;
import com.zhihu_lvliangi_android.domain.HospitalRecord;
import com.zhihu_lvliangi_android.option.parent.ParentFragment;
import com.zhihu_lvliangi_android.util.Utils;

public class MotherFragment extends ParentFragment implements OnClickListener {

	private  View mParentView;
	private TextView mTextView;
	private List<String> mTitles;
	private int currentPos = -1;
	private General_item items;
	private List mCureRecords; //门诊或者住院记录
	private List<HealthRecord> mHealthRecords;
	
	@Override
	protected int getLayoutId() {
		return R.layout.fragment_zhengwu_theme;
	}

	@Override
	protected void setupViews(View parentView) {
		initData();
		mParentView =parentView;
		mTitles = Arrays.asList(getResources().getStringArray(R.array.parent_items));
		initView();
	}
	private void initData() {
		mCureRecords = new ArrayList();
		mHealthRecords = new ArrayList<HealthRecord>();
		HospitalRecord record ;
		record = new HospitalRecord();
		record.setName("李曾芳");
		record.setType("内科");
		record.setInDate("2014/9/28");
		record.setOutDate("2014/9/28");
		record.setHospital("吕梁市第一医院");
		record.setResults("主诊断：脑梗塞  \r\n 次诊断： 高血压");
		record.setCures("辅助检查\r\n一级护理\r\n输液");
		record.setSuggestions("注意饮食清淡\r\n忌烟忌酒忌辛辣");
		record.setCaseInfo("入院记录\r\n主任查房记录\r\n日常病程记录\r\n危重病历讨论\r\n出院记录");
		record.setFee("27.3");
		
		record.setTrementDate("2014/9/28");
		mCureRecords.add(record);
		
		HealthRecord hrecord = new HealthRecord();
		hrecord.setDate("2014/7/18");
		hrecord.setHealthGuide("建议每年体检2次");;
		hrecord.setHosptail("城东社区卫生站");
		hrecord.setName("李曾芳");
		hrecord.setResult("高血压，心律不齐");
		hrecord.setSex("女");
		record.setTrementDate("2014/9/27");
		mHealthRecords.add(hrecord);
		
		hrecord = new HealthRecord();
		hrecord.setDate("2014/7/18");
		hrecord.setHealthGuide("建议每年体检2次");;
		hrecord.setHosptail("城东社区卫生站");
		hrecord.setName("李曾芳");
		hrecord.setResult("高血压，心律不齐");
		hrecord.setSex("女");
		
		
		record.setTrementDate("2014/9/28");
		mHealthRecords.add(hrecord);
	}
	
	private void initView() {
		LayoutInflater inflater = LayoutInflater.from(getActivity());
		LinearLayout container =(LinearLayout) mParentView.findViewById(R.id.container);

		for (int i = 0; i < mTitles.size(); i++) {
			View view = inflater.inflate(R.layout.fragment_mother_item, null);
			view.setTag(i);
			view.setOnClickListener(this);
			mTextView= (TextView) view.findViewById(R.id.department_item_title);
			mTextView.setText(mTitles.get(i));
			view.findViewWithTag(MotherFragment.this.getClass().getSimpleName()).setId(Utils.generateViewId());
			container.addView(view, i);
		}
	}

	@Override
	public void onClick(View v) {
		try {
			int pos = Integer.parseInt(v.getTag().toString());
			changeItemForCategory(pos,v);
		} catch (NumberFormatException e) {
			//TODO 处理v.getid()
			throw new RuntimeException(e);
		}		
	}

	@Override
	protected void initialized(Bundle savedInstanceState) {

	}

	@Override
	public void threadTask() {

	}

	public static ParentFragment newInstance() {
		return new MotherFragment();
	}
	
	private void changeItemForCategory(final int pos, final View v) {
		if (currentPos ==pos ) {
			Animation anim = AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_min_arrow_reverse);
			v.findViewById(R.id.mini_arrow).startAnimation(anim);
			anim.setAnimationListener(new AnimationListener() {
				
				@Override
				public void onAnimationStart(Animation animation) {
					
				}
				
				@Override
				public void onAnimationRepeat(Animation animation) {
					
				}
				
				@Override
				public void onAnimationEnd(Animation animation) {
					if (items==null) {
						return;
					}
					getActivity().getSupportFragmentManager().beginTransaction().
					setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE).
					remove(items).
					commit();
					currentPos = -1;
				}
			});
		}else {
			Animation anim =AnimationUtils.loadAnimation(getActivity(), R.anim.rotate_min_arrow);
			v.findViewById(R.id.mini_arrow).startAnimation(anim);
			anim.setAnimationListener(new AnimationListener() {
				
				@Override
				public void onAnimationStart(Animation animation) {
					
				}
				
				@Override
				public void onAnimationRepeat(Animation animation) {
					
				}
				
				@SuppressWarnings("unchecked")
				@Override
				public void onAnimationEnd(Animation animation) {
					int id  = v.findViewWithTag(MotherFragment.this.getClass().getSimpleName()).getId();
					items = General_item.newInstance(pos == 0? mCureRecords:mHealthRecords);
					getActivity().getSupportFragmentManager().beginTransaction().
					//R.anim.push_left_in,R.anim.push_left_out,R.anim.push_right_in,R.anim.push_right_out
					setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN).
					replace(id, items).
					commit();
					currentPos = pos;
				}
			});
			
		}
	}

}
